<?php

/*
 *
 */

class Model_User_Administrator extends Model_BaseModel
{
    use Lib_BaseTraits;
    /**
     * 缓存前缀
     *
     * @var unknown
     */
    private static $tag = 'Model_User_Administrator';
    /**
     * Undocumented variable
     *
     * @var string
     */
    private $table = "sys_administrator";

    public function __construct()
    {
        // 选择连接的数据库
        parent::_init('mediation');
    }

    /**
     * 获取全部数据
     *
     * @return array
     */
    public function getListAll(): array
    {
        $sql = "SELECT * FROM {$this->table}";
        $data = [];
        $res = $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, $data)->fetchAll();
        return $res ? $res : [];
    }

    /**
     * 根据手机号查询管理员信息
     *
     * @param string $mobile
     * @return array
     */
    public function getByMobile(string $mobile): array
    {
        $sql = "SELECT * FROM {$this->table} where mobile=:mobile";
        $data = array(
            ':mobile' => $mobile,
        );
        $res = $this->dao->conn()->noCache()->preparedSql($sql, $data)->fetchOne();
        return $res ? $res : [];
    }

    /**
     * @param $jobNumber
     * @return mixed
     */
    public function getByJobNumber($jobNumber)
    {
        $sql = "SELECT * FROM {$this->table} where workerId=:workerId";
        $data = array(
            ':workerId' => $jobNumber,
        );
        $res = $this->dao->conn()->noCache()->preparedSql($sql, $data)->fetchOne();
        return $res ? $res : [];
    }




    /**
     * 根据UID查询用户信息
     *
     * @param integer $id
     * @return array
     */
    public function getById(int $id): array
    {
        $sql = "SELECT * FROM {$this->table} where id=:id";
        $data = array(
            ':id' => $id,
        );
        $res = $this->dao->conn()->noCache()->preparedSql($sql, $data)->fetchOne();
        return $res;
    }

    /**
     * 根据角色查询信息
     *
     * @param integer $role_id
     * @return array
     */
    public function getByRoleId($role_id, $status)
    {
        $data = [];
        $sql = "SELECT * FROM {$this->table} where role_id=:role_id";
        $data[':role_id'] = $role_id;
        if ($status) {
            $sql .= " AND status=:status";
            $data[':status'] = $status;
        }

        $res = $this->dao->conn()->noCache()->preparedSql($sql, $data)->fetchAll();
        return $res;
    }

    /**
     * @param $mobile
     * @param $pwd
     * @return array
     */
    public function getAdminByMobilePwd($mobile, $pwd)
    {
        $sql = "SELECT * FROM {$this->table} WHERE mobile=:mobile AND password=:password";
        $data = [
            ':mobile' => $mobile,
            ':password' => $pwd,
        ];
        $res = $this->dao->conn()->noCache()->preparedSql($sql, $data)->fetchOne();
        return $res ? $res : [];
    }

    /**
     * @param $id
     * @param $randomKey
     * @return mixed
     */
    public function updateRandomKeyById($id, $randomKey)
    {
        $sql = "UPDATE {$this->table} SET random_key=:random_key WHERE id=:id";
        $data = [
            ':id' => $id,
            ':random_key' => $randomKey
        ];
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        if ($res) {
            $this->dao->clearTag(self::$tag);
        }
        return $res;
    }

    /**
     * @param array $fields
     * @param array $search
     * @param bool $cache
     * @return mixed
     */
    public function getAllBySearch($fields = array(), $search = array(), $cache = false)
    {
        $fields = empty($fields) ? '*' : implode(',', $fields);
        $search_str = '';
        if (isset($search['where']) && !empty($search['where'])) {
            $search_str .= 'WHERE ' . implode(' AND ', $search['where']);
        }
        if (isset($search['order']) && $search['order']) {
            $search_str .= ' ORDER BY ' . $search['order'];
        }
        if (isset($search['limit']) && $search['limit']) {
            $search['start'] = isset($search['start']) ? $search['start'] : 0;
            $search_str .= ' LIMIT ' . $search['start'] . ',' . $search['limit'];
        }
        $sql = "SELECT {$fields} FROM {$this->table} {$search_str}";
        $cache ? $this->dao->conn()->setTag(self::$tag) : $this->dao->conn(false)->noCache();
        $res = $this->dao->preparedSql($sql, array())->fetchAll();
        return $res;
    }

    /*
     * 存储数据
     */
    public function insertData($saveData)
    {
        foreach ($saveData as $key => $value) {
            $arrFields[] = '`' . $key . '`';
            $temp = ':' . $key;
            $dataA[$temp] = $value;
            $arrValues[] = $temp;
        }
        $sqlA = 'INSERT INTO `' . $this->table . '` (' . implode(',', $arrFields) . ') VALUES (' . implode(',', $arrValues) . ')';
        $uid = $this->dao->conn(false)->noCache()->preparedSql($sqlA, $dataA)->lastInsertId();
        $this->dao->clearTag(self::$tag);
        return $uid;
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getAdminById($id)
    {
        $sql = "SELECT * FROM {$this->table} WHERE id=:id";
        $data = [
            ':id' => $id
        ];
        return $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, $data)->fetchOne();
    }

    /**
     * @param $id
     * @return mixed
     */
    public function deleteById($id)
    {
        $sql = "DELETE FROM {$this->table} WHERE id=:id";
        $data = [
            ':id' => $id,
        ];
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        if ($res) {
            $this->dao->clearTag(self::$tag);
        }
        return $res;
    }

    /**
     * @param $updateData
     * @return mixed
     */
    public function updateItemById($updateData)
    {
        $sql = "UPDATE {$this->table} SET name=:name,mobile=:mobile,status=:status,role_id=:role_id,update_time=:update_time,workerId=:workerId,workerId2=:workerId2,mediationOrganId=:mediationOrganId,dataType=:dataType,allowOrganId=:allowOrganId,avatarImg=:avatarImg,showWorkerId=:showWorkerId WHERE id=:id";
        $data = [
            ':id' => $updateData['id'],
            ':name' => addslashes($updateData['name']),
            ':mobile' => $updateData['mobile'],
            ':role_id' => $updateData['role_id'],
            ':status' => $updateData['status'],
            ':workerId' => $updateData['workerId'],
            ':workerId2' => $updateData['workerId2'],
            ':avatarImg' => empty($updateData['avatarImg']) ? '' : $updateData['avatarImg'],
            ':mediationOrganId' => isset($updateData['mediationOrganId']) ? $updateData['mediationOrganId'] : 0,
            ':dataType' => isset($updateData['dataType']) ? $updateData['dataType'] : 0,
            ':allowOrganId' => isset($updateData['allowOrganId']) ? $updateData['allowOrganId'] : 0,
            ':showWorkerId' => isset($updateData['showWorkerId']) ? $updateData['showWorkerId'] : 0,
            ':update_time' => time(),
        ];
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        if ($res) {
            $this->dao->clearTag(self::$tag);
        }
        return $res;
    }

    /**
     * @param $id
     * @param $pwd
     * @return mixed
     */
    public function updatePwdById($id, $pwd)
    {
        $sql = "UPDATE {$this->table} SET password=:password,update_time=:update_time WHERE id=:id";
        $data = [
            ':id' => $id,
            ':password' => $pwd,
            ':update_time' => time(),
        ];
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        if ($res) {
            $this->dao->clearTag(self::$tag);
        }
        return $res;
    }

    /**
     * @param $role_id
     * @return mixed
     */
    public function getAdminByRoleId($role_id)
    {
        $sql = "SELECT * FROM {$this->table} WHERE role_id=:role_id";
        $data = [
            ':role_id' => $role_id
        ];
        return $this->dao->conn()->noCache()->preparedSql($sql, $data)->fetchOne();
    }
}
